package cn.dxh.stagefinal.controller;

import cn.dxh.stagefinal.bean.Department;
import cn.dxh.stagefinal.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * IntelliJ IDEA -> stagefinal
 * 2019/12/10 9:44
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Controller
public class DepartmentController
{
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/record/departmentlist.html")
    public String  departmentlist(ModelMap map){
        List<Department> departments = departmentService.findDepartments();
        map.addAttribute("departments",departments);
        return  "/record/departmentlist";
    }

    @RequestMapping("/record/deletedepartment.html")
    public void  deletedepartmentByDid(Integer dId,HttpServletResponse response){
        try {
            int len = departmentService.deleteDepartmentByDid(dId);
            response.setContentType("text/html;charset= utf-8");
            PrintWriter out= response.getWriter();
            if (len > 0 ){
                out.println("<script> alert('部门删除成功');location.href='/record/departmentlist.html'</script>");
            }  if (len ==  -1  ){
                out.println("<script> alert('该部门不能删除,有用户使用着');location.href='/record/departmentlist.html'</script>");
            }
            else {
                out.println("<script> alert('部门删除失败');location.href='/record/departmentlist.html';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/record/insertdepartment.html")
    public String insert(){
        return  "/record/insertdepartment";
    }

    @RequestMapping("/record/insertdep.do")
    public void insertDepartment(Department department, HttpServletResponse response){
        try {
            int len = departmentService.insertDepartment(department);
            response.setContentType("text/html;charset= utf-8");
            PrintWriter out= response.getWriter();
            if (len > 0 ){
                out.println("<script> alert('部门添加成功');location.href='/record/departmentlist.html'</script>");
            }
            else {
                out.println("<script> alert('部门添加失败');location.href='/record/insertdepartment.html';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/record/updatedepartment.html")
    public String updatedep(Integer dId ,ModelMap  map){
        Department department= departmentService.findDepartmentByDid(dId);
        map.addAttribute("department",department);
        return  "/record/updatedepartment";
    }

    @RequestMapping("/record/updatedepartment")
    public void  updatedepartment(Department department,HttpServletResponse response ){
        try {
            int len = departmentService.updatedepartment(department);
            response.setContentType("text/html;charset= utf-8");
            PrintWriter out= response.getWriter();
            if (len > 0 ){
                out.println("<script> alert('部门修改成功');location.href='/record/departmentlist.html'</script>");
            }
            else {
                Integer dId = department.getdId();
                out.println("<script> alert('部门修改失败');location.href='/record/updatedepartment.html?dId="+dId+"';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
